// firstPackage/pages/expertModule/expertList/expertList.js

import { queryAllExpertList } from "../../../../service/expert_service"
let app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    themeStyle: "",
    expertList: [

    ],
    showExpertPop: false,
    expertPopArr: [


    ],
    cityName: "",
    isShowLoading: false,
    isBottomShow: false,
    page: 0,
    pageSize: 20,
    searchWords: "",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      cityName: app.globalData.requestCity,
    })
    this.queryAllExpertList();
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (app.globalData.themeStyle) {
      this.setData({
        themeStyle: app.globalData.themeStyle
      })
    }
  },
  //查询所有专家
  queryAllExpertList() {
    wx.showLoading();
    let pageNum = this.data.page;
    let pageSize = this.data.pageSize;
    queryAllExpertList(this.data.page, this.data.pageSize, this.data.searchWords, this.data.cityName).then(res => {
      wx.hideLoading();
      console.log("JJLog:专家列表:" + JSON.stringify(res));
      if (res.code == 200) {
        let tempArr = [];
        tempArr = this.data.expertList;
        if (tempArr.length > 0 && pageNum > 0) {
          //加载下一页:
          tempArr = tempArr.concat(res.data);
          if (res.data.length < pageSize) {
            this.setData({
              isShowLoading: false,
              isBottomShow: true,
            })
            this.setData({
              expertList: tempArr
            })
          } else if (res.data.length == pageSize) {
            this.setData({
              isShowLoading: false,
            })
            this.setData({
              expertList: tempArr
            })
          }
        } else {
          this.setData({
            isShowLoading: false,
          })
          this.setData({
            expertList: res.data
          })
          if (res.data.length < pageSize) {
            this.setData({
              isBottomShow: true
            })
          }
        }
      }
    }).catch(err => {
      wx.hideLoading();
      let page = this.data.page;
      if (page > 0) {
        page = page - 1;
      }
      this.setData({
        isShowLoading: false,
        page: page
      })
      wx.showToast({
        title: err || "查询失败",
        icon: "none"
      })
      console.error("JJLog:queryAllExpertList", err);
    })
  },
  //搜索回调:
  searchAction(e) {
    this.setData({
      searchWords: e.detail.searchWord,
      page: 0,
      isBottomShow: false,
    })
    this.queryAllExpertList();
  },

  //置业顾问聊天点击
  expertChatAction(e) {
    if (!app.globalData.isLogin) {
      wx.navigateTo({
        url: '/pages/login/login',
      })
      return;
    }
    let item = e.currentTarget.dataset.item;
    let chatData = {
      name: item.realName,
      avatar: item.avatar,
      chatUserId: item.openId,
      conversationType: "C2C",
      houseNumber: "",
      houseName: "",
      expertId: item.id,
      expertPhone: item.phone || "",
      expertWeChat: item.wxChat || "",
    }
    app.globalData.chatData = chatData;
    wx.navigateTo({
      url: '/firstPackage/pages/msgModule/chatRoom/chatRoom',
    })

  },
  callPhoneAction(e) {
    let item = e.currentTarget.dataset.item;
    let phone = item.phone;
    wx.makePhoneCall({
      phoneNumber: phone,
      fail: err => {

      }
    })
  },
  //跳转到详情:
  pushToDetail(e) {
    let id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/firstPackage/pages/expertModule/newExpertDetail/newExpertDetail?id=' + id,
    })
  },
  //scrollView滚动
  scrolltolower() {
    if (this.data.isBottomShow) {
      //已经到底了就不加载了
      return;
    }
    let tempPage = this.data.page + 1;
    this.setData({
      page: tempPage,
      isShowLoading: true,
    })
    this.queryAllExpertList();
  },
  //隐藏弹窗
  exportPopHidden() {
    this.setData({
      showExpertPop: false
    })
  },

  //复制职业顾问微信号:
  copyWxChatAction(e) {
    let text = e.currentTarget.dataset.text;
    wx.setClipboardData({
      data: text,
      success: function (res) {
        wx.getClipboardData({
          success: function (res) {
            wx.showToast({
              title: '复制微信号成功'
            })
          },
          fail: function (err) {
            wx.showToast({
              title: '复制失败',
              icon: "none"
            })
          }
        })
      }
    })

  },










})